/**
 * Program  : ViewPagerActivity.java
 * Author   : qianj
 * Create   : 2012-5-31 下午2:02:15
 *
 * Copyright 2012 by newyulong Technologies Ltd.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of newyulong Technologies Ltd.("Confidential Information").  
 * You shall not disclose such Confidential Information and shall 
 * use it only in accordance with the terms of the license agreement 
 * you entered into with newyulong Technologies Ltd.
 *
 */

package cn.learn.base.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import cn.learn.base.BaseActivity;
import cn.learn.base.IndexActivity;
import cn.learn.base.R;

/**
 * 
 * @author qianj
 * @version 1.0.0
 * @2012-5-31 下午2:02:15
 */
public class ViewPagerActivity extends BaseActivity {

	List<View> listViews;

	Context context = null;

	LocalActivityManager manager = null;

	TabHost tabHost = null;

	private ViewPager pager = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setView(R.layout.viewpager);
		SetTitle("滑动tabHost");
		setTitleBar(R.drawable.title_back, "", R.drawable.title_home, "");
		
		

		context = ViewPagerActivity.this;

		pager  = (ViewPager) findViewById(R.id.viewpage);
		listViews = new ArrayList<View>();

		// 切换布局的方式
		// listViews.add(View.inflate(getApplicationContext(), R.layout.sub,
		// null));

		manager = new LocalActivityManager(this, true);
		manager.dispatchCreate(savedInstanceState);

		tabHost = (TabHost) findViewById(R.id.tabhost);
		tabHost.setup();
		tabHost.setup(manager);
		
		
		
		RelativeLayout tabIndicator1 = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tabwidget, null);  
		TextView tvTab1 = (TextView)tabIndicator1.findViewById(R.id.tv_title);
		tvTab1.setText("第一页");
		
		RelativeLayout tabIndicator2 = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tabwidget,null);  
		TextView tvTab2 = (TextView)tabIndicator2.findViewById(R.id.tv_title);
		tvTab2.setText("第二页");
		
		RelativeLayout tabIndicator3 = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tabwidget,null);  
		TextView tvTab3 = (TextView)tabIndicator3.findViewById(R.id.tv_title);
		tvTab3.setText("第三页");
	
		tabHost.addTab(tabHost.newTabSpec("A").setIndicator(tabIndicator1).setContent(new Intent(context,T3Activity.class)));
		tabHost.addTab(tabHost.newTabSpec("B").setIndicator(tabIndicator2).setContent(new Intent(context, T3Activity.class)));
		tabHost.addTab(tabHost.newTabSpec("C").setIndicator(tabIndicator3).setContent(new Intent(context, T1Activity.class)));

		
		
		Intent i1 = new Intent(context, T1Activity.class);
		listViews.add(getView("A", i1));
		Intent i2 = new Intent(context, T2Activity.class);
		listViews.add(getView("B", i2));
		Intent i3 = new Intent(context, T3Activity.class);
		listViews.add(getView("C", i3));
		
		tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {
				 tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			            @Override
			            public void onTabChanged(String tabId) {
			                if ("A".equals(tabId)) {
			                    pager.setCurrentItem(0);
			                } 
			                if ("B".equals(tabId)) {
			                    pager.setCurrentItem(1);
			                } 
			                if ("C".equals(tabId)) {
			                    pager.setCurrentItem(2);
			                } 
			            }
			        });
				
			}
		});
		

		pager .setAdapter(new MyPageAdapter(listViews));
		pager .setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				tabHost.setCurrentTab(arg0);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
	

	}

	private View getView(String id, Intent intent) {
		return manager.startActivity(id, intent).getDecorView();
	}

	private class MyPageAdapter extends PagerAdapter {

		private List<View> list;

		private MyPageAdapter(List<View> list) {
			this.list = list;
		}

		@Override
        public void destroyItem(View view, int position, Object arg2) {
            ViewPager pViewPager = ((ViewPager) view);
            pViewPager.removeView(list.get(position));
        }

        @Override
        public void finishUpdate(View arg0) {
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object instantiateItem(View view, int position) {
            ViewPager pViewPager = ((ViewPager) view);
            pViewPager.addView(list.get(position));
            return list.get(position);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
        }

	}

	@Override
	protected void HandleTitleBarEvent(int buttonId) {
		
		switch (buttonId) {
		case 0:
			finish();
			break;
		case 1:
			Intent i = new Intent(ViewPagerActivity.this,IndexActivity.class);
			startActivity(i);
			break;
		default:
			break;
		}
		
	}

}
